// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: EHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 40 : TYPE_EHZSETUP

// Function declarations
reserve function EHZSetup_SpeedUpMusic1P
reserve function EHZSetup_SlowDownMusic1P
reserve function EHZSetup_SpeedUpMusic2P
reserve function EHZSetup_SlowDownMusic2P

private alias saveRAM[494] : saved.bossGameMode //7B8 SData.bin
private alias saveRAM[495] : saved.bossType //7BC SData.bin
private alias saveRAM[496] : saved.BossMusic //7C0 SData.bin
private alias saveRAM[497] : saved.Character //7C4 SData.bin
private alias saveRAM[498] : saved.TimeLimit //7C8 SData.bin
private alias saveRAM[499] : saved.superMode //7CC SData.bin

// Static Values
public value EHZSetup_value33 = 0;

private alias EHZSetup_value33 : EHZSetup_AchievementValue

// Tables
public table EHZSetup_table39
	508, 24, 509, 10, 510, 12, 511, 24, 510, 12, 509, 10
end table

public table EHZSetup_table40
	512, 128, 513, 20, 512, 8, 513, 8, 512, 8, 513, 8
end table

public table EHZSetup_table41
	514, 128, 515, 12, 514, 12, 515, 12, 514, 6, 515, 6, 514, 6, 515, 6
end table

public table EHZSetup_table42
	516, 8, 517, 8
end table

public table EHZSetup_table43
	518, 128, 519, 8, 518, 8, 519, 8, 518, 8, 519, 12, 518, 12, 519, 12
	
end table

public table EHZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

private table EHZSetup_demoTable1
	0x440000, 0x2910000, 32, 1, 40, 27, 8, 39, 56, 1, 40, 9, 8, 40, 56, 1, 40, 6, 8, 8, 56, 1, 40, 6, 8, 175, 56, 1, 40, 7, 8, 111, 0, 2, 4, 35, 52, 1, 36, 7, 4, 73, 0, 4, 8, 4, 56, 1, 40, 7, 8, 68, 0, 5, 4, 30, 0, 8, 2, 4, 50, 1, 34, 1, 50, 1, 32, 4, 0, 15, 8, 56, 0, 1, 2, 32, 50, 1, 34, 1, 42, 1, 40, 3, 8, 255, 8, 97, 0, 12, 4, 5, 0, 26, 8, 21, 56, 1, 40, 9, 8, 255, 8, 4, 10, 3, 2, 7, 10, 2
end table

private table EHZSetup_demoTable2
	0x440000, 0x2950000, 32, 13, 40, 38, 8, 47, 56, 1, 40, 12, 8, 224, 56, 1, 40, 5, 8, 69, 56, 1, 40, 8, 32, 2, 0, 2, 8, 12, 0, 28, 8, 16, 0, 1, 8, 255, 8, 5, 56, 1, 40, 24, 8, 218, 56, 1, 40, 9, 8, 50, 0, 1, 4, 7, 0, 16, 8, 174, 56, 1, 40, 12, 8, 7, 56, 1, 40, 8, 8, 4, 56, 1, 40, 5, 8, 3, 4, 2, 52, 1, 36, 5, 4, 4, 52, 1, 32, 5, 0, 4, 4, 1, 52, 1, 36, 3, 4, 4, 52, 1, 36, 4, 4, 5, 52, 1, 36, 3
	4, 1, 0, 2, 8, 2, 56, 1, 40, 3, 32, 2, 0, 1, 8, 5, 0, 5, 8, 3, 56, 1, 40, 2, 8, 1, 0, 9, 8, 5, 0, 15, 8, 22, 0, 3, 4, 2, 52, 1, 36, 9, 32, 2, 40, 8, 8, 4, 56, 1, 40, 4, 32, 1, 0, 3, 48, 1, 32, 6, 0, 4, 48, 1, 32, 5, 0, 1, 8, 3, 0, 1, 48, 1, 32, 5, 0, 3, 48, 1, 32, 3, 40, 3, 8, 2, 0, 1, 48, 1, 32, 5, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 5, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 3, 48, 1, 32, 5, 0, 3, 48, 1, 32, 6, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 40, 2, 8, 39, 56, 1, 40, 12, 8, 13, 0, 17, 8, 4, 0, 12, 4, 11, 8, 8
end table

private table EHZSetup_demoTable3
	0x440000, 0x2910000, 0, 18, 8, 77, 56, 1, 40, 13, 8, 70, 56, 1, 40, 14, 8, 3, 0, 22, 4, 4, 0, 3, 48, 1, 32, 16, 40, 3, 8, 1, 0, 30, 48, 1, 32, 38, 40, 149, 8, 11, 0, 3, 4, 6, 0, 4, 4, 5, 0, 5, 8, 13, 0, 14, 4, 89, 0, 6, 8, 44, 10, 1, 2, 13, 0, 52, 8, 59, 56, 1, 40, 17, 8, 18, 0, 4, 4, 33, 0, 1, 8, 105, 56, 1, 40, 30, 8, 45, 4, 6, 0, 52, 8, 6, 56, 1, 40, 19, 8, 54, 10, 6, 2, 17, 10, 2, 8, 58, 56, 1, 40, 4, 8, 197, 56, 1, 41, 10, 9, 14, 57, 1, 41, 40, 33, 4, 1, 126, 9, 12, 8, 6, 56, 1, 40, 12, 
end table

function EHZSetup_SpeedUpMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80, 0x1F40)
			break
		case 2
			SetMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function EHZSetup_SlowDownMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("EmeraldHill.ogg", 0, 0x254AE, 0x30D4)
			break
		case 2
			SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function EHZSetup_SpeedUpMusic2P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4, 0x1F40)
			break
		case 2
			SetMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function EHZSetup_SlowDownMusic2P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("EmeraldHill2.ogg", 0, 0x184F4, 0x30D4)
			break
		case 2
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	if object.state < 5
		object.value1++
		if object.value1 == 8
			object.value1 = 0
			GetPaletteEntry(0, 159, temp1)
			GetPaletteEntry(0, 158, temp0)
			SetPaletteEntry(0, 159, temp0)
			GetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 158, temp0)
			GetPaletteEntry(0, 147, temp0)
			SetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 147, temp1)
		end if
	end if
	object.value4++
	if object.value4 > 7
		tileLayer[1].deformationOffset++
		object.value4 = 0
	end if
	if object.value5 < 2
		GetTableValue(temp0, object.value6, EHZSetup_table39)
		Copy16x16Tile(502, temp0)
		temp0 += 12
		Copy16x16Tile(503, temp0)
		temp0 += 4
		Copy16x16Tile(504, temp0)
		object.value6++
		GetTableValue(object.value5, object.value6, EHZSetup_table39)
		object.value6++
		object.value6 %= 12
	else
		object.value5--
	end if
	if object.value7 < 2
		GetTableValue(temp0, object.value8, EHZSetup_table40)
		Copy16x16Tile(764, temp0)
		object.value8++
		GetTableValue(object.value7, object.value8, EHZSetup_table40)
		object.value8++
		object.value8 %= 12
	else
		object.value7--
	end if
	if object.value9 < 2
		GetTableValue(temp0, object.value10, EHZSetup_table41)
		Copy16x16Tile(765, temp0)
		object.value10++
		GetTableValue(object.value9, object.value10, EHZSetup_table41)
		object.value10++
		object.value10 &= 15
	else
		object.value9--
	end if
	if object.value11 < 2
		GetTableValue(temp0, object.value12, EHZSetup_table42)
		Copy16x16Tile(766, temp0)
		object.value12++
		GetTableValue(object.value11, object.value12, EHZSetup_table42)
		object.value12++
		object.value12 &= 3
	else
		object.value11--
	end if
	if object.value13 < 2
		GetTableValue(temp0, object.value14, EHZSetup_table43)
		Copy16x16Tile(767, temp0)
		object.value14++
		GetTableValue(object.value13, object.value14, EHZSetup_table43)
		object.value14++
		object.value14 &= 15
	else
		object.value13--
	end if
	if EHZSetup_AchievementValue == false
		if stage.debugMode == false
			if specialStage.emeralds >= 127 // Not 127 emeralds, it's 01111111, or seven ones, in bits
				if options.gameMode == 0
					EHZSetup_AchievementValue = true
					CallNativeFunction2(SetAchievement, 2, 100) // Get all emeralds in Emerald Hill achievment
				end if
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					arrayPos1 += 4
					if saveRAM[arrayPos1] < 20
						EHZSetup_AchievementValue = true
						CallNativeFunction2(SetAchievement, 2, 100)
					end if
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	if stage.activeList != BONUS_STAGE
		if options.vsMode == false
			SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
			SpeedUpMusic = EHZSetup_SpeedUpMusic1P
			SlowDownMusic = EHZSetup_SlowDownMusic1P
		else
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SpeedUpMusic = EHZSetup_SpeedUpMusic2P
			SlowDownMusic = EHZSetup_SlowDownMusic2P
		end if
	else
		switch saved.BossMusic
		case 0
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			break
		case 1
			SetMusicTrack("DeathEgg.ogg", 0, 1)
			break
		case 2
			SetMusicTrack("DeathEgg2.ogg", 0, 8203)
			break
		case 3
			SetMusicTrack("MetallicMadness.ogg",0,146030)
			break
		case 5
		case 7
			SetMusicTrack("TitanicMonarch1.ogg", 0, 184069)
			break
		case 4
		case 6
			SetMusicTrack("TitanicMonarch2.ogg", 0, 384873)
			break
		case 8
			SetMusicTrack("LaunchBase2.ogg", 0, 692772)
			break
		case 9
			SetMusicTrack("SkySanctuary.ogg", 0, 5322)
			break
		case 10
			SetMusicTrack("PanicPuppet1.ogg", 0, 1158705)
			break
		case 11
		case 12
			SetMusicTrack("PanicPuppet2.ogg", 0, 12818)
			break
		case 13
			SetMusicTrack("TheMachine.ogg", 0, 17651)
			break
		endswitch
	end if
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Ricky]
	tileLayer[1].scrollPos = -0x80000
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, EHZSetup_table38)
		arrayPos0++
	loop

	// Cycle through all Title Card objects
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Title Card]
			if saved.bossGameMode == 0
				object[arrayPos0].type = 0
			endif
		endif
		arrayPos0++
	loop
	

	object[10].type = TypeName[EHZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	SetPaletteEntry(0, 192, 0)
	if options.attractMode == true
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = EHZSetup_demoTable1
			PlayerObject_ReplaySize = 110
			PlayerObject_ReplayLength = 0x654
			break
		case 1
			PlayerObject_ReplayTable = EHZSetup_demoTable2
			PlayerObject_ReplaySize = 318
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = EHZSetup_demoTable3
			PlayerObject_ReplaySize = 138
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
